<?php
/*------------------------------------------------------------------------------

{}	[]
http//webappkit.net
------------------------------------------------------------------------------*/
/**
* defines 
*/
class wakTrigger extends databean {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	* @param
	* @return
	*/
	function wakTrigger($properties=array()) {
		$this->__construct($properties);
	}/**/
	
	/**
	* "real" constructor
	*/
	function __construct($properties=array()) {
		$lib=&wak::getservice('waklib');
		$lib->needClass('wakparameters');
		$this->structureLock(false);
		parent::__construct($properties);
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* tells if trigger matches http request
	* @param wakHttpRequest $req
	* @return bool
	*/
	function matchRequest(&$req) {
		$deb=&wak::getservice('wakdebug');
		// method
		//echo wakdebug::vardump($this);
		if ($this->method and $this->method!=$req->getMember('method')) {
			//echo "trigger ".$deb->vardump($this)." don't apply to ".$deb->vardump($req);
			return false;
		}
		$url=$req->getMember('url');
		// path
		if ($this->uri_path) {
			// apply_children
			$path=$url->getpath(); //echo "<p>path $path</p>";
			switch (true) {
				case $this->uri_path=='/'.$path and !$this->apply_self:
					// doesn't apply self
					return false;
					break;
				case $this->uri_path=='/'.$path and $this->apply_self:
					// does apply self
					//return false;
					break;
				case ereg('^'.$this->uri_path,'/'.$url->path) and $this->apply_children:
					break;
				default:
					//echo "trigger ".$deb->vardump($this)." don't apply to ".$deb->vardump($req);
					return false;
			}
		}
		//echo "trigger ".$deb->vardump($this)." does apply to ".$deb->vardump($req);
		return true;
	}
	
	/**
	* Runs trigger event.
	* retrieves parameters, gets event handler, run event and routes result
	* @access public
	* @return bool
	*/
	function run() {
		$debug=&wak::getservice('wakdebug');
		//echo '<h1>'.$this->getEventCall().'</h1>';
		//echo $debug->vardump($this);
		
		// retrieving parameters
		
		//echo $debug->vardump($this->parameters);
		$params=$this->get('parameters');
		//var_dump($params);
		//echo wakapi::vardump($params);
		
		// running event
		$eventCall=$this->getEventCall();
		// echo '<p>step '.$GLOBALS['wak_step'].', priority '.$this->getpriority().' : running '.$event.'</p>';
		$evM=&wak::getservice('wakevents');
		$result=&$evM->run($eventCall,$params);
		
		// routing result
		if ($route=$this->get('result_to')) {
			$router=&wak::getservice('wakdatarouter');
			$router->sendDataTo($result,$route);
		}
		
		return true;
	}

	// getters
	/**
	*
	*/
	function getStep() {
		return $this->step;
	}
	/**
	*
	*/
	function getEventCall() {
		return $this->event_call;
	}
	
	/**
	*
	*/
	function getPriority() {
		return $this->priority;
	}
	
	/**
	*
	*/
	function getParameters() {
		return $this->parameters;
	}
	
	/**
	*
	*/
	function getResultTo() {
		return $this->result_to;
	}
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	var $_databean_declare_first=true;
	
	var $id;
	
	var $step=6; // 1 to 7
	function _step_check(&$s) {
		return in_array($s,array(1,2,3,4,5,6,7));
	}
	
	var $method;
	function _method_inputfilter($x) { return strtoupper($x); }
	function _method_check($x) {
		return in_array($x,array('GET','POST','PUT','DELETE'));
	}
	
	var $priority=100;
	function _priority_check(&$p) {
		return is_int($p) and $p>0 and $p<256;
	}
	
	var $uri_path='/';
	function _uri_path_check(&$u) {
		return ereg('(/[a-zA-Z0-9_.-]*)+',$u);
	}
	
	var $apply_self=true;
	function _apply_self_check(&$a) {
		return is_bool($a);
	}
	
	var $apply_children=false;
	function _apply_children_check(&$a) {
		return is_bool($a);
	}
	
	var $event_call;
	function _event_call_check(&$e) {
		return is_string($e);
	}
	function _event_call_required() {return true;}
	//var $_event_call_check_ereg='[a-z]';
	
	var $parameters;
	function _parameters_check(&$p) {
		return is_a($p,'wakparameters');
	}
	/**
	* formatting parameters, getting a wakparameters instance from serialized string
	* @param $parameters
	* @access protected
	* @return wakparameters
	*/
	function & _parameters_inputfilter($parameters=null) {
		if (is_a($parameters,'wakparameters')) {
			// already an instance
			return $parameters;
		} else if (is_string($parameters) and strlen($parameters)>0) {
			// echo $parameters;
			// unserialization
			$parameters=@unserialize($parameters);
			//var_dump($parameters);
			return $parameters;
		}
		// $deb=&wak::getservice('wakdebug');
		// echo $deb->vardump($parameters);
		return $parameters;
	}
	
	/**
	* @var string where to forward result. see wakdatarouter.service doc
	*/
	var $result_to;
	
	var $comments;
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------

------------------------------------------------------------------------------*/
}
?>
